#!/usr/bin/env python
## Special thanks to jimmyromanticdevil to let me use his code :p
## Thanks to MaXe for the help at Miburl 
import urllib
import urllib2
import re
import sys
import os
#########################################################################
#                                                                       #
# Copyright (C) 2010 LeXeL                                              #
#                                                                       #
# This program is free software: you can redistribute it and/or modify  #
# it under the terms of the GNU General Public License as published by  #
# the Free Software Foundation, either version 3 of the License, or     #
# any later version.                                                    #
#                                                                       #
# This program is distributed in the hope that it will be useful,       #
# but WITHOUT ANY WARRANTY; without even the implied warranty of        #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
# GNU General Public License for more details.                          #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with this program.  If not, see <http://www.gnu.org/licenses/>. #
#########################################################################
def main():
    os.system(clear())#clear the screen
    title()#print the title
    argc = len(sys.argv)
    if argc > 2:
        print "[Error!]"
        print sys.argv[0]+" <URL>"
        sys.exit(1)
    else:
        try:#Separete the http:// from the url and grab the url only
            url = sys.argv[1]
            url = url.split('//')
            if len(url) > 1:
                furl = url[1]
            elif len(url) == 1:
		furl = url[0]
        except:
            print "[Error!]"
            print sys.argv[0]+" <URL>"
            sys.exit(1)
    printer(furl)
def printer(url):#print out every result from the url fuctions
    print "\nResults: "
    print "[Goo.gl] "+ google(url)
    print "[Bit.ly] "+ bitly(url)
    print "[Tiny]   "+ tinyurl(url)
    print "[Is.gd]  "+ isgd(url)
    print "[Miburl] "+ miburl(url)
    print "[Alturl] "+ shorturl(url)
def clear():#Comand to clear the screen
    if sys.platform == 'linux-i386' or sys.platform == 'linux2' or sys.platform == 'darwin':
        SysCls = 'clear'
    elif sys.platform == 'win32' or sys.platform == 'dos' or sys.platform[0:5] == 'ms-dos':
        SysCls = 'cls'
    else:
        SysCls = 'unknown'
    return SysCls
def builder():#build the urlopener
    opener = urllib2.build_opener(urllib2.HTTPHandler())
    opener.addheaders = [('User-agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')]
    urllib2.install_opener(opener)
    
##this Fuctions are the ones ho get the data from the URL's    
def google(url):
    goo = 'http://goo.gl/api/url?url=%s' % urllib.quote(url)
    page = urllib2.urlopen(goo, data='')
    find = page.read()
    found = re.findall("\"short_url\":\"(http://goo.gl/\w+)",find)
    found = ''.join(found)
    return found
def bitly(url):
    bit = "http://api.bit.ly/v3/shorten?login=sh0rter&apiKey=R_e3bc46da7e3df6982b52488012da2ed7&longUrl=http://%s&format=txt" %(url)
    page = urllib2.urlopen(bit).read()
    bit = re.findall("(http://bit.ly/\w+)",page)
    bit = ''.join(bit)
    return bit
def isgd(url):
    isgd = "http://is.gd/api.php?longurl=%s" %(url)
    page = urllib2.urlopen(isgd).read()
    isgd = re.findall("(http://is.gd/\w+)",page)
    isgd = ''.join(isgd)
    return isgd    
def tinyurl(url):
    tiny = "http://tinyurl.com/api-create.php?url=%s" %(url)
    page = urllib2.urlopen(tiny).read()
    tiny = re.findall("(http://tinyurl.com/\w+)", page)
    tiny = ''.join(tiny)
    return tiny
def miburl(url):#MibUrl have a special type of encryptation called "multipartform-data"
    #Thnx to http://stacyprowell.com/blog/2009/05/29/handling-multipartform-data-in-python/
    
    try:
        from multipart import Multipart
        imp = True
    except ImportError:
        output = "you dont have the correct module\n please download it at: http://lexel.t35.com/multipart.py"
        imp = False
    if imp == True:
        m = Multipart()
        m.field('url','http://'+url)
        ct,body = m.get()
        mib = "http://miburl.com/index.php"
        request = urllib2.Request(mib,headers = {'Content-Type':ct},data=body)
        reply = urllib2.urlopen(request)
        rep = reply.read()
        find  = re.findall("class=minified>(http://miburl.com/\w+)</span>",rep)
        find = ''.join(find)
        output = find
        return output
    elif imp == False:
        return output
def shorturl(url):
    shurl = "http://shorturl.com/make_url.php?longurl=%s" %(url)
    page = urllib2.urlopen(shurl).read()
    shurl = re.findall("<a href=(http://alturl.com/\w+) target=\"_blank\">", page)
    shurl = ''.join(shurl)
    return shurl
def title():
    tit = """
        .__    _______          __                  
  ______|  |__ \   _  \________/  |_   ____ _______
 /  ___/|  |  \/  /_\  \_  __ \   __\_/ __ \\\_  __ \\ 
 \___ \ |   Y  \  \_/   \  | \/|  |  \  ___/ |  | \/
/____  >|___|  /\_____  /__|   |__|   \___  >|__|   
     \/      \/       \/                  \/        
                                                    v1.5
    Made by : lexelEZ[at]gmail[dot]com
                forums.intern0t.net
"""
    print tit
        
if __name__ == '__main__':
    
    main()
